/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-27 14:23:17
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-25 09:00:27
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { MoadifManageServiceProxy, BomMakeContentServiceProxy, StoreServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { AddOrEdditComponent } from './add-or-eddit/add-or-eddit.component';

@Component({
    selector: 'app-warehouse-setting',
    templateUrl: './warehouse-setting.component.html',
    styleUrls: ['./warehouse-setting.component.less']
})
export class WarehouseSettingComponent extends AppComponentBase {
    @ViewChild('AddOrEdditComponent') AddOrEdditComponent: AddOrEdditComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    rowdata: any = {};
    title = '';
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
        private _storeServiceProxy: StoreServiceProxy,
    ) {
        super(injector);
    }
    update(row) {
        this.rowdata = row;
        this.title = "仓库修改";
        setTimeout(() => {
            this.AddOrEdditComponent.show();
        }, 10);
    }
    delate(row) {
        this.message.confirm(
            '确定要删除这条数据吗？',
            (isConfirmed) => {
                if (isConfirmed) {
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this._storeServiceProxy
                        .deleteStoreContent(row.id)
                        .subscribe((result) => {
                            this.saveFlag = true;
                            if (result.code == 0) {
                                this._messageService.success('删除成功');
                                this.getTableList();
                            } else {
                                this._messageService.error(result.message)
                            }
                        });
                    }
                }
            }
        );
        
    }
    ngOnInit() {

    }
    add() {
        this.title = "新增仓库";
        setTimeout(() => {
            this.AddOrEdditComponent.show();
        }, 10);
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._storeServiceProxy.getStoreContentList(
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.rowdata = {};
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

}
